package com.hdl.service.impl;

import com.hdl.*;
import com.hdl.config.ConfigurationCenter;
import com.hdl.dao.ResumeDao;
import com.hdl.handler.UserServerHandler;
import com.hdl.service.UserService;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 类名 UserServiceImpl
 * 描述 TODO
 *
 * @author hedonglin
 * @version 1.0
 * @date 2020/6/10 10:39
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private ResumeDao resumeDao;

    @Value("${netty.server.port}")
    private Integer port;

    @Override
    public String sayHello(String word) {
        try {
            Thread.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "端口号："+ port +" ，调用成功--参数：" + word;
    }

    @Override
    public String sayHello2(String name, String word) {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  "端口号："+ port +" " + name + "----" + word;
    }


    @Override
    public String getResumeById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap = resumeDao.getResumeById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "端口号："+ port +"----查询数据库结果返回" + (resultMap.size() == 0 ? "null" : resultMap.get("name"));
    }


    public static void startServer(String hostName, int port) {
        try {
            System.out.println("RPC服务器"+hostName+"启动成功, 运行端口为："+port);
            NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    // 拆包器
                    p.addLast(new PackageSpliter());
                    // JSON解析器
                    p.addLast(new RpcDecode(RpcRequest.class, new JSONSerializer()));
                    p.addLast(new StringEncoder());
                    p.addLast(new UserServerHandler());
                }
            });
            bootstrap.bind(hostName, port).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
